﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Serialization;

public partial class ListFiles : System.Web.UI.Page
{
    private const string ccContentTypeXml = "application/xml";

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Buffer = true;
        Response.ContentType = ccContentTypeXml;
        // Response.AddHeader("Content-Disposition", String.Format("attachment;filename={0}", doc.docFileName))
        // Response.Write();
        if (Request["t"] == "xml" )
            Response.Write(GetTabFilesList(@".\tabs\", "*.xml"));
        else if (Request["t"] == "pdf")
            Response.Write(GetPdfFilesList(@"..\Guitar\", "*.pdf"));
    }
    StringBuilder GetPdfFilesList(string path, string ext)
    {
        StringBuilder res = new StringBuilder();
        var location = Server.MapPath(path);
        var list = System.IO.Directory.GetFiles(location, ext);
        var format = @"<TabFile><Title>{0}</Title><Name>{1}</Name></TabFile>";

        res.Append(@"<ArrayOfTabFile");
        res.Append(@" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""");
        res.AppendLine(@" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">");
        foreach (var file in list)
        {
            var item = file.Replace(location, "");
            res.AppendFormat(format, item.Replace(".pdf" , ""), item);
        }
        res.AppendLine(@"</ArrayOfTabFile>");
        return res;
    }
    StringBuilder GetTabFilesList(string path, string ext)
    {
        StringBuilder res = new StringBuilder();
        var location = Server.MapPath(path);
        var list = System.IO.Directory.GetFiles(location, ext);
        var format = @"<TabFile><Title>{0}</Title><Name>{1}</Name></TabFile>";

        res.Append(@"<ArrayOfTabFile");
        res.Append(@" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""");
        res.AppendLine(@" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">");
        foreach (var file in list)
        {
            var item = LoadFrom<Song>(file);
            res.AppendFormat(format, item.Title , file.Replace(location , "") );
        }
		//res.AppendFormat(format,location , location );
        res.AppendLine(@"</ArrayOfTabFile>");
        return res;
    }
    public T LoadFrom<T>(string fullPath) where T : new()
    {
        T instance;
        if (File.Exists(fullPath))
        {
            string xml = File.ReadAllText(fullPath);
            //var serializer = new Serializer<T>();
            var stream = new XmlTextReader(new StringReader(xml));
            var serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(stream);
        }
        else
        {
            instance = new T();
        }

        return instance;
    }

}

public class Song
{
    public string Title { get; set; }
    //public string Legend { get; set; }
    //public string Content { get; set; }
    //public string YouTube { get; set; }
}